/**
 * <strong>Title : EquipmentCycleTime.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>@类注释说明写在此处@<br>
 * <strong>Create on : 2015年11月4日 下午4:55:40<br>
 * </strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
package cn.b.sky.mes.em.controller;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.condition.EquipmentCondition;
import cn.b.sky.mes.em.model.condition.EquipmentCycleTimeCondition;
import cn.b.sky.mes.em.service.EquipmentCycleTimeService;
import cn.b.sky.mes.em.service.EquipmentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.em.model.Equipment;
import cn.b.sky.mes.em.model.EquipmentCycleTime;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * <strong>Title : EquipmentCycleTimeController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.controller<br></strong>
 * <strong>Description : </strong>设备周期时间控制器<br> 
 * <strong>Create on : 2015年11月4日 下午4:55:40<br></strong>
 * <p>
 * @author dapeng.li<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/equipmentCycleTime/*")
public class EquipmentCycleTimeController extends SecurityController {

  @Resource
  EquipmentCycleTimeService equipmentCycleTimeService;

  @Resource
  EquipmentService equipmentService;

  @RequestMapping("equipmentCycleTime_list")
  public String selectEquipmentCycleTimeList(String navTabId, Model model,
      @ModelAttribute("condition") EquipmentCycleTimeCondition condition) {
    condition.setSiteCode(getSiteCode());
    List<EquipmentCycleTime> list = equipmentCycleTimeService.selectEquipmentCycleTimeListByCondition(condition);
    int count = equipmentCycleTimeService.selectEquipmentCycleTimeCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/equipmentCycleTime/equipmentCycleTime_list";
  }

  @RequestMapping("addEquipmentCycleTime")
  public String addEquipmentCycleTime(String navTabId, Model model, String id) {
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "add");
    return "em/equipmentCycleTime/equipmentCycleTime_add";
  }

  @RequestMapping("modifyEquipmentCycleTime")
  public String modifyEquipmentCycleTime(String navTabId, Model model, String id) {
    EquipmentCycleTime cause = equipmentCycleTimeService.getEquipmentCycleTime(id, getSiteCode());
    model.addAttribute("equipmentCycleTime", cause);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "modify");
    return "em/equipmentCycleTime/equipmentCycleTime_edit";
  }

  @RequestMapping("validateEquipmentCycleTimeCode")
  @ResponseBody
  public boolean validateEquipmentCycleTimeCode(String equipmentCycleTimeCode) {
    if (StringUtils.isEmpty(equipmentCycleTimeCode)) {
      return true;
    }
    EquipmentCycleTime equipmentCycleTime = equipmentCycleTimeService.getEquipmentCycleTime(equipmentCycleTimeCode,
        getSiteCode());
    return equipmentCycleTime == null;
  }

  @RequestMapping("viewEquipmentCycleTime")
  public String viewEquipmentCycleTime(String navTabId, Model model, String id) {
    EquipmentCycleTime cause = equipmentCycleTimeService.getEquipmentCycleTime(id, getSiteCode());
    model.addAttribute("equipmentCycleTime", cause);
    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("pattern", "read");
    return "em/equipmentCycleTime/equipmentCycleTime_edit";
  }

  @RequestMapping("saveEquipmentCycleTime")
  @ResponseBody
  public DWZResult saveEquipmentCycleTime(String navTabId, Model model, String id, EquipmentCycleTime equipmentCycleTime) {
    if (equipmentCycleTime == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    if (equipmentCycleTime.getLockFlag() == null) {
      equipmentCycleTime.setLockFlag(0);
    }
    setCreateInfo(equipmentCycleTime);
    equipmentCycleTime.setSiteCode(getSiteCode());
    if (StringUtils.isBlank(id)) {
      equipmentCycleTimeService.insertEquipmentCycleTime(equipmentCycleTime);
    } else {
      equipmentCycleTimeService.updateEquipmentCycleTime(equipmentCycleTime);

    }
    return dialogAjaxDone(
        getImeViewMessageI18n(null, "successfulOperation.M"),
        navTabId, null, null, null);

  }

  @RequestMapping("deleteEquipmentCycleTime")
  @ResponseBody
  public DWZResult deleteEquipmentCycleTime(HttpServletRequest request, String navTabId) {
    String ids = request.getParameter("ids");
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    String[] equipmentCycleTimeCodes = ids.split(",");
    int len = equipmentCycleTimeCodes.length;
    if (len == 0) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    BatchJobReport[] reports = new BatchJobReport[len];
    boolean success = true;
    for (int i = 0; i < len; i++) {
      reports[i] = equipmentCycleTimeService.deleteEquipmentCycleTime(equipmentCycleTimeCodes[i], getSiteCode());
      if (!reports[i].isSuccess()) {
        success = false;
      }
    }
    if (success) {
      //直接返回成功消息
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null);
    } else {
      return batchjobAjaxDone(request, Arrays.asList(reports));
    }
  }

  @RequestMapping("equipmentCycleTimeLookup")
  public String equipmentCycleTimeLookup(String navTabId, Model model,
      @ModelAttribute("condition") EquipmentCycleTimeCondition condition) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<EquipmentCycleTime> list = equipmentCycleTimeService.selectEquipmentCycleTimeListByCondition(condition);
    int count = equipmentCycleTimeService.selectEquipmentCycleTimeCountByCondition(condition);
    condition.setData(list);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/equipmentCycleTime/equipmentCycleTime_lookup";
  }

  @RequestMapping("validateEquipmentCode")
  @ResponseBody
  public boolean validateEquipmentCode(String equipmentCode) {
    EquipmentCycleTime equipmentCycleTime = equipmentCycleTimeService.getEquipmentCycleTime(equipmentCode,
        getSiteCode());
    Equipment equipment = equipmentService.getEquipment(equipmentCode, getSiteCode());
    return equipment != null && equipment.getLockFlag() != 1 && equipmentCycleTime == null;
  }

  @RequestMapping("equipmentSuggest")
  @ResponseBody
  public Object equipmentSuggest(@ModelAttribute("condition") EquipmentCondition condition) {
    condition.setSiteCode(this.getSiteCode());
    condition.setLockFlag(0);
    condition.setOrderField("CreateDateTime");
    //不分页查询设备周期表中不存在的所有设备
    List<Equipment> equipmentList = equipmentCycleTimeService.selectAllEquipmentListForSuggest(condition);
    for (Equipment e : equipmentList) {
      if (StringUtils.isBlank(e.getUseDepartment())) {
        e.setUseDepartment("");
      }
    }
    return equipmentList;
  }
}
